<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 
 * @param 
 *         $xxx = 作用
 * @static 
 *         XXX = 作用
 * 
 * 
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');
include(SERVER_ROOT_PATH.'/include/config/ml_search_config.php');

class ml_mq_search_user extends MqClass{
    const QUEUE_NAME = ML_QUEUENAME_SEARCH_USER;
    const SEARCH_RSYNC_FILE_DIR = ML_SEARCH_TMP_USER_DIR;
    private $oUserinfo;
    
    
public function __construct($queue)
    {
        $this->oUserinfo = ml_biz_user_getinfo::get_instance();
        
        parent::__construct($queue);
    }

    public function run_job(){
        //接收的数据
        $arr = $this->src_data;
        
        $DF = $arr['dealtype']; 
        $uid = $arr['uid'];
        
        if (!ml_tool_isuid::is_mluid($uid)) {
            return ;
        }
        
        $mlCdata['uid'] = $uid;
        $mlCdata['DF'] = $DF;
        
        switch ($DF) {
            case 'D':
                break;
            case 'A':           
                
                $this->oUserinfo->initUser($uid);
                $this->oUserinfo->getUserFeedCount();
                $this->oUserinfo->getUserFollowCount();
                $this->oUserinfo->getUserNick();
                $this->oUserinfo->getUserSkillTags(false);
                $this->oUserinfo->getUserVoteCount();
                $this->oUserinfo->getUserStatus();
                
                $data = $this->oUserinfo->get_result();
                
                if (empty($data)) {
                    return ;
                }
                
                $mlCdata['uid'] = $uid;
                $mlCdata['nickname'] = $data['nickname'];
                $mlCdata['stags'] = implode(',', $data['skill_tags']);
                $mlCdata['status'] = $data['status'];
                if ($data['status']==1 && $data['verify_email']==0) {
                    $mlCdata['status'] = 10;
                }

                $att = $data['VoteCount'];
                $fan = $data['FollowCount'];
                $meila = $data['FeedCount'];
                $popular = ml_tool_hotrank::calc_search_user_popular($att, $fan, $meila);
                
                $mlCdata['rtime'] = 0;
                $mlCdata['popular'] = $popular;
                break;
            default:
                return ;    
        }
        
        $push_msg = $this->parseLogFomat($mlCdata);
        if ($push_msg !== false) {
                $this->writeToFile($push_msg);
        }
        
        
        
        return true;
    }
    
    
    /**
     * 写入日志文件
     *  
     */
    public function writeToFile( $str ){
 
        $filename = self::SEARCH_RSYNC_FILE_DIR.'meila_user_'.date("Y-m-d_H:i:s").'.txt';
        if (!is_dir(self::SEARCH_RSYNC_FILE_DIR)) mkdir(self::SEARCH_RSYNC_FILE_DIR, 0700, true);
        
        $fp = fopen($filename, "ab");        
        // 进行排它型锁定
        if (flock($fp, LOCK_EX)) { 
            fwrite($fp, $str);
            flock($fp, LOCK_UN); 
            fclose($fp);
            return true; 
        } else {
            fclose($fp);
            return false;
        }  
    }
    
    
  /**
     * 解析数据成要推送的格式
     * @param array $data
     */
    public function parseLogFomat( $data ){
        if( !$data['uid'] ){
            return false;
        }
           
        $push_msg  = "@\r\n";
           foreach( $data as $key=>$val ){
               $key = trim( $key );
               $val = trim( $val ); 
               //如果反序列化后是个数组，不直接转发，而是对数组里的value转发后，再序列化回去             
               
               $array = unserialize( $val );
               
               if(is_array( $array ) ){                          
                   $arrayNew = $this->encodeArray( $array );
                   $val = serialize($arrayNew);
               }else{
                   $val = mb_convert_encoding( $val, 'GBK' , 'utf-8' );
               }
               $push_msg .= "@".$key.":".$val." \r\n";
           }
           $push_msg .= "@\r\n";
           return $push_msg;       
    }
    
    /**
     * 
     * 递归把数组里的内容都转换成gbk编码
     * @param $array
     */
    public function encodeArray( $array ){
        $arrayTmp = array();
        foreach ($array as $k=>$v ){
            if(is_array($v)){
                $arrayTmp[$k] = $this->encodeArray( $v );
            }else{
                $arrayTmp[$k] = mb_convert_encoding( $array[$k], 'GBK' , 'utf-8' );
            }
        }
        return $arrayTmp;
    }

}

ml_run_queue_check(ml_mq_search_user::QUEUE_NAME);
$xblog_obj = new ml_mq_search_user(new McQueue(ml_mq_search_user::QUEUE_NAME));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();